package com.android.lovingpet.common.userinfo;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.common.postlist.PostListAdapter;
import com.android.lovingpet.common.postlist.PostListPresenter;
import com.bumptech.glide.Glide;
import com.android.lovingpet.common.userlist.UserListActivity;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.util.Utility;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserInfoActivity extends BaseActivity<UserInfoContract.View, UserInfoPresenter> implements View.OnClickListener, UserInfoContract.View {

    private List<Post> posts;
    private PostListAdapter adapter;
    private SmartRefreshLayout refreshLayout;
    private CircleImageView ivHead;
    private TextView tvLevel;
    private ImageView ivSex;
    private TextView tvLike;
    private TextView tvContribution;
    private TextView tvFans;
    private TextView tvAttentions;
    private TextView tvFollow;
    private Account account;
    private ArrayList<Integer> fans;
    private ArrayList<Integer> attentions;
    private int mode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ((TextView) findViewById(R.id.tv_title)).setText("详情");
        ivHead = findViewById(R.id.iv_head);
        tvLevel = findViewById(R.id.tv_level);
        ivSex = findViewById(R.id.iv_sex);
        tvLike = findViewById(R.id.tv_like);
        tvContribution = findViewById(R.id.tv_contribution);
        tvFans = findViewById(R.id.tv_fans);
        tvAttentions = findViewById(R.id.tv_attention);
        tvFollow = findViewById(R.id.tv_follow);
        account = getIntent().getParcelableExtra("account");
        Random random = new Random(account.getRealId());
        tvLike.setText(random.nextInt(10000) + 2000 + "");

        //ViewPager viewPager = findViewById(R.id.viewPager_info);
        //viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), account.getRealId()));
        //TabLayout tabLayout = findViewById(R.id.tabLayout_info);
        //tabLayout.setupWithViewPager(viewPager);

        posts = new ArrayList<>();
        adapter = new PostListAdapter(posts);
        RecyclerView recyclerView = findViewById(R.id.rv_info);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        PostListPresenter presenter = new PostListPresenter(1);
        presenter.attachView(this);
        refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //getPresenter().getPosts(account.getRealId(), 0);
                presenter.getPosts(account.getRealId(), 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                presenter.getPosts(account.getRealId(), posts.size());
                //getPresenter().getPosts(account.getRealId(), posts.size());
            }
        });

        findViewById(R.id.ll_like).setOnClickListener(this);
        findViewById(R.id.ll_contribution).setOnClickListener(this);
        findViewById(R.id.ll_fans).setOnClickListener(this);
        findViewById(R.id.ll_attention).setOnClickListener(this);
        tvFollow.setOnClickListener(this);
        findViewById(R.id.tv_chat).setOnClickListener(this);
        setInfo();
        getPresenter().getFans(account.getRealId());
        getPresenter().getAttentions(account.getRealId());

        presenter.getPosts(account.getRealId(), 0);
    }

    @Override
    public UserInfoPresenter createPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    public UserInfoContract.View createView() {
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_fans:
                if (this.fans == null) {
                    showToast("加载中...");
                    return;
                }
                Intent fans = new Intent(UserInfoActivity.this, UserListActivity.class);
                fans.putIntegerArrayListExtra(UserListActivity.IDS_KEY, this.fans);
                fans.putExtra(UserListActivity.TITLE_KEY, UserListActivity.TITLE_FANS);
                startActivity(fans);
                break;

            case R.id.ll_attention:
                if (attentions == null) {
                    showToast("加载中...");
                    return;
                }
                Intent attention = new Intent(UserInfoActivity.this, UserListActivity.class);
                attention.putIntegerArrayListExtra(UserListActivity.IDS_KEY, attentions);
                attention.putExtra(UserListActivity.TITLE_KEY, UserListActivity.TITLE_ATTENTIONS);
                startActivity(attention);
                break;

            case R.id.tv_follow:
                getPresenter().followOrCancel(account.getRealId(), mode);
                break;

            case R.id.tv_chat:
                break;

            default:
                break;
        }
    }

    @Override
    public void setFans(final ArrayList<Integer> fans) {
        this.fans = fans;
        runOnUiThread(() -> tvFans.setText(fans.size() + ""));
    }

    @Override
    public void setAttentions(final ArrayList<Integer> attentions) {
        this.attentions = attentions;
        runOnUiThread(() -> tvAttentions.setText(attentions.size() + ""));
    }

    @Override
    public void setButtonText(final boolean isFollowed) {
        runOnUiThread(() -> {
            if (isFollowed) {
                tvFollow.setText("取消关注");
                mode = 1;
            } else {
                tvFollow.setText("关注");
                mode = 0;
            }
        });
    }

    @Override
    public void addPosts(List<Post> posts) {
        this.posts.addAll(posts);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearPosts() {
        posts.clear();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setUser() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void refresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //recyclerView.refresh();
                refreshLayout.autoRefresh();
            }
        });
    }

    @Override
    public void complete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //recyclerView.refreshComplete();
                //recyclerView.loadMoreComplete();
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }

    void setInfo() {
        Glide.with(this).load(account.getHead()).into(ivHead);
        tvLevel.setText(account.getGrade() + "");
        if (account.getGender() == 0) {
            Glide.with(this).load(R.drawable.ic_female).into(ivSex);
        } else {
            Glide.with(this).load(R.drawable.ic_male).into(ivSex);
        }
        //tvLike.setText();
        tvContribution.setText(account.getLoveValue() + "");
        setFollowed();
    }

    void setFollowed() {
        setButtonText(false);
        ArrayList<Integer> ids = Utility.getAccount().getLikes();
        if (ids == null) {
            return;
        }
        for (int i : ids) {
            if (i == account.getRealId()) {
                setButtonText(true);
                break;
            }
        }
    }
}
